package com.yizhubao.app.activitys;

import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;

import com.yizhubao.app.R;
import com.yizhubao.app.base.BaseActivityToolBar;
import com.yizhubao.app.beans.ConfigBean;
import com.yizhubao.app.models.ConfigModel;
import com.yizhubao.app.utils.ConfigObserver;
import com.yizhubao.app.utils.Constants;

import butterknife.BindView;
import cn.jzvd.JzvdStd;
import h.framework.pacy.utils.LogUtil;
import h.framework.pacy.utils.ScreenUtil;
import h.framework.pacy.utils.ToastUtil;

/**
 * 创建人: PACY
 * 创建时间: 6/6/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 说明页面
 */
public class ExplainActivity extends BaseActivityToolBar {

    @BindView(R.id.welfare_tv)
    TextView welfare_tv;

    @BindView(R.id.contact_tv)
    TextView contact_tv;

    @BindView(R.id.jz_video)
    JzvdStd jzvdStd;

    @BindView(R.id.video_cv)
    CardView video_cv;
    ConfigModel model;

    @Override
    protected int setContentLayout() {
        return R.layout.activity_explain;
    }

    @Override
    protected void initView() {
        setTitleOnLeftIcon(R.drawable.icon_back,"");
        initWelfareText();
        initContactText();
        ViewGroup.LayoutParams params=video_cv.getLayoutParams();
        params.width= ScreenUtil.getScreenWidth(ExplainActivity.this)-ScreenUtil.dp2px(44);
        double temp=(double)params.width/16;
        params.height=(int)(temp*9)-1;
        video_cv.setLayoutParams(params);
        LogUtil.i("params.width="+params.width+",params.height="+params.height);
    }

    @Override
    protected void initData() {
        model=new ConfigModel();
        sendRequest();
    }

    void sendRequest(){
        model.getConfig(new ConfigObserver() {
            @Override
            public void onSuccess(ConfigBean t) {
                if(t.isShowWhyPayVideo()){
                    video_cv.setVisibility(View.VISIBLE);
                    jzvdStd.setUp(Constants.WHY_VIDEO
                            , "");
                    jzvdStd.startVideo();
                }else{
                    video_cv.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errMessage) {

            }
        },getLifeSubject());
    }

    /**
     * 初始化公益服务文本
     */
    private void initWelfareText(){
        String str="3，如果您是国家“特殊岗位人员”，请点击这里，会由国家出钱，让您免费使用遗嘱宝。";
        SpannableStringBuilder span = new SpannableStringBuilder(str);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                startActivity(ExplainActivity.this, WelfareActivity.class);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.btn_color_blue));
            }
        },18,22, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        welfare_tv.setMovementMethod(LinkMovementMethod.getInstance());
        welfare_tv.setText(span);
    }
    /**
     * 初始化联系客服文本
     */
    private void initContactText(){
        String str="5，其它问题，请点击查看详细服务说明或者联系平台客服。";
        SpannableStringBuilder span = new SpannableStringBuilder(str);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Bundle bundle=new Bundle();
                bundle.putString("title","常见问题");
                bundle.putString("url",Constants.QUESTION);
                startActivity(ExplainActivity.this, WebActivity.class,bundle);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.btn_color_blue));
            }
        },12,18, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Bundle bundle=new Bundle();
                bundle.putString("title","联系客服");
                bundle.putString("url", Constants.LIANXIKEFU);
                startActivity(ExplainActivity.this, WebActivity.class,bundle);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.btn_color_blue));
            }
        },20,27, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        contact_tv.setMovementMethod(LinkMovementMethod.getInstance());
        contact_tv.setText(span);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jzvdStd.releaseAllVideos();
        if (jzvdStd.backPress()) {
            return;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        jzvdStd.releaseAllVideos();
    }
}